package com.teradata.service;

import com.teradata.entities.User;
import com.teradata.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserMapper userMapper;


    /*public List<User> getUsers() {
        return userMapper.getUsers();
    }*/
    public List<User> getUsers() {
        return userMapper.selectAll();
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }


    public List<User> getUserList() {
        return jdbcTemplate.query("SELECT * FROM user", new RowMapper<User>() {
            @Nullable
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setAge(resultSet.getInt("age"));
                user.setCreate(resultSet.getInt("create"));
                return user;
            }
        });
    }

    public List<Map<String, Object>> getUserListToMapDef() {
        return jdbcTemplate.query("SELECT * FROM user", new RowMapper<Map<String, Object>>() {
            @Nullable
            @Override
            public Map<String, Object> mapRow(ResultSet resultSet, int i) throws SQLException {
                Map<String, Object> userMap = new HashMap<String, Object>();
                ResultSetMetaData rsmd = resultSet.getMetaData();
                int count = rsmd.getColumnCount();
                for (i = 1; i <= count; i++) {
                    String key = rsmd.getColumnLabel(i);
                    Object value = resultSet.getObject(i);
                    userMap.put(key, value);
                }
                return userMap;
            }
        });
    }

    public List<Map<String, Object>> getUserListToMapByName() {
        return jdbcTemplate.query("SELECT * FROM user", new RowMapper<Map<String, Object>>() {
            @Nullable
            @Override
            public Map<String, Object> mapRow(ResultSet resultSet, int i) throws SQLException {
                Map<String, Object> userMap = new HashMap<String, Object>();

                while (resultSet.next()) {
                    resultSet.getInt("id");

                }

                ResultSetMetaData rsmd = resultSet.getMetaData();
                int count = rsmd.getColumnCount();
                for (i = 1; i <= count; i++) {
                    String key = rsmd.getColumnLabel(i);
                    Object value = resultSet.getObject(i);
                    userMap.put(key, value);
                }
                return userMap;
            }
        });
    }

    public List<Map<String, Object>> getUserListMap() {
        return jdbcTemplate.queryForList("SELECT * FROM user");
    }

    /*public User getUserById(int id){
        return userMapper.getUserById(id);
    }*/

    public User getUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public int saveUser(User user) {
        return userMapper.insert(user);
    }

    @Transactional
    public void copyUser(int id, String name) {
        User user = getUserById(id);
        int i = jdbcTemplate.update("INSERT INTO user (name,age,`create`) VALUES (?,?,?)", new Object[]{user.getName(), user.getAge(), user.getCreate()});
        logger.info("复制成功: i=" + i + " name=" + name + " id=" + id);
        i = jdbcTemplate.update("UPDATE user SET name=? WHERE id=?", new Object[]{name, user.getId()});
        logger.info("更新成功: i=" + i + " name=" + name + " id=" + id);
    }
}
